package com.hyg.base.ui.common;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hyg.base.R;
import com.hyg.base.ui.adapter.MainTabAdapter;
import com.hyg.base.ui.base.BaseActivity;
import com.hyg.base.ui.base.BaseFragment;
import com.hyg.base.ui.base.BasePresenter;
import com.hyg.base.widget.NoScrollViewPager;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Fragment容器
 * Created by huangyuguang on 2018/5/10.
 */

public class FragmentContainerActivity extends BaseActivity {
    public static final String TAG = FragmentContainerActivity.class.getSimpleName();
    private static FragmentContainerActivity instance;

    public static final String CLASSNAME = "className";
    public static final String BUNDLE = "bundle";

    private TextView mTvTitle;

    private NoScrollViewPager mVpContent;

    protected static FcTypeEnum jjbType;

    protected int mContentViewId = 0;
    public BaseFragment fragment = null;

    public Bundle mBundle;
    private List<BaseFragment> mFragments;
    private MainTabAdapter mTabAdapter;

    public enum FcTypeEnum {
        Normal,
        NoTitleBar,
    }

    public static FragmentContainerActivity getInstance(){
        return FragmentContainerActivity.getInstance(FcTypeEnum.Normal);
    }
    public static FragmentContainerActivity getInstance(FcTypeEnum type){
        jjbType = type;
        if (instance == null){
            synchronized (FragmentContainerActivity.class){
                if (instance == null){
                    instance = new FragmentContainerActivity();
                }
            }
        }
        return instance;
    }

    public Class<? extends FragmentContainerActivity> getActivityClass(){
        return FragmentContainerActivity.class;
    }

    public void launch(Activity activity, Class<? extends Fragment> clazz) {
        launch(activity,clazz,null);
    }

    public void launch(Activity activity, Class<? extends Fragment> clazz, Bundle bundle) {
        Intent intent = new Intent(activity, getActivityClass());
        intent.putExtra(CLASSNAME, clazz.getName());
        if (bundle != null)
            intent.putExtra(BUNDLE, bundle);
        activity.startActivity(intent);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return mContentViewId;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        String className = getIntent().getStringExtra(CLASSNAME);
        if (TextUtils.isEmpty(className)) {
            finish();
            return;
        }

        mContentViewId = R.layout.comm_fragment_container;
        mBundle = getIntent().getBundleExtra(BUNDLE);
        //Fragment fragment = null;
        Class clazz = null;
        if (savedInstanceState == null) {
            try {
                clazz = Class.forName(className);
                fragment = (BaseFragment) clazz.newInstance();

                if(mBundle != null){
                    try {
                        Method method = clazz.getMethod("setArguments", new Class[] { Bundle.class });
                        method.invoke(fragment, mBundle);
                    } catch (Exception e) {
                        //LogUtils.e(TAG,e.getMessage(), e);
                    }
                }

//                // 重写Activity的主题
//                try {
//                    Method method = clazz.getMethod("setTheme");
//                    if (method != null)
//                        overrideTheme = Integer.parseInt(method.invoke(fragment).toString());
//                } catch (Exception e) {
//                	LogUtils.e(e.getMessage(), e);
//                }

                // 重写Activity的contentView 需要在fragment里实现该方法
                try {
                    Method method = clazz.getMethod("setNewFragmentContainerView");
                    if (method != null)
                        mContentViewId = Integer.parseInt(method.invoke(fragment).toString());
                } catch (Exception e) {
                    //LogUtils.e(TAG,e.getMessage(), e);
                }
            } catch (Exception e) {
                e.printStackTrace();
                finish();
                return;
            }
        }
        super.onCreate(savedInstanceState);
        mVpContent = findViewById(R.id.vp_content);
        mFragments = new ArrayList<>(1);
        mFragments.add(fragment);
        mTabAdapter = new MainTabAdapter(mFragments, getSupportFragmentManager());
        mVpContent.setAdapter(mTabAdapter);
        mVpContent.setOffscreenPageLimit(mFragments.size());
//        getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, fragment, TAG).commit();

        if (jjbType == FcTypeEnum.NoTitleBar) {
            findViewById(R.id.lay_title_bar).setVisibility(View.GONE);
            return;
        }

        View btnBack = findViewById(R.id.v_back);
        if (btnBack != null) {
            btnBack.setOnClickListener(v -> finish());
        }

        mTvTitle = findViewById(R.id.tv_title);
        if(mTvTitle != null && fragment != null){
            mTvTitle.setVisibility(View.GONE);
            setNavBarTitle(fragment.initTitle());
        }

        // 需要自定义标题栏其他内容在这里初始化
        try {
            Method method = clazz.getDeclaredMethod("initTitleView");
            method.setAccessible(true);
            if (method != null) method.invoke(fragment,findViewById(R.id.lay_title_bar));
        } catch (Exception e) {
            //LogUtils.e(TAG,e.getMessage(), e);
        }
    }

    public void setNavBarTitle(String str) {
        if (mTvTitle != null) {
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(str);
        }
    }
}
